package com.dongjiantong.web.protal.controller.farmrecords;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.web.util.UserUtil;
import com.dongjiantong.farmrecords.enums.FarmRecordsNumberStatus;
import com.dongjiantong.farmrecords.model.FarmRecordsInspection;
import com.dongjiantong.farmrecords.service.FarmRecordsInspectionService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.service.AnimalTypeService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 防疫中心-检验检测记录(政府端)
 */

@Slf4j
@OpenBean
public class FarmRecordsInspectionController {

    @Reference(version = "1.0.0")
    public FarmRecordsInspectionService farmRecordsInspectionService;
    @Reference(version = "1.0.3")
    public AnimalTypeService animalTypeService;

    /**
     * 根据养殖场ids,起止时间或者状态查询检验检测记录 (条件都可以为空,表示查询所有)
     * @param areaList 请求的地区List
     * @param farmId  请求的养殖场id
     * @param startDate 请求的查询开始时间
     * @param endDate   请求的查询结束时间
     * @param pageNum
     * @param pageSize
     * @param type 请求的检测项目
     * @param productType
     * @return  Response<PageInfo<FarmRecordsInspection>>
     */
    @OpenMethod(key = "web.farmrecordsinspection.selectByFarmIdAndDate",
            paramNames = {"areaList","farmId","startDate","endDate","pageNum","pageSize","type","productType"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<FarmRecordsInspection>> selectByFarmIdAndDate(List<Integer> areaList,
                                                                           @RequestParam(required = false) Long farmId,
                                                                           @RequestParam(required = false) String startDate,
                                                                           @RequestParam(required = false) String endDate,
                                                                           @RequestParam(required = false) Integer pageNum,
                                                                           @RequestParam(required = false) Integer pageSize,
                                                                           @RequestParam(required = false) Integer type,
                                                                           @RequestParam(required = false) List<Integer> productType){
        if (farmRecordsInspectionService == null){
            throw new OPServerException("请检查 farmRecordsInspectionService 服务");
        }
        if (animalTypeService == null){
            throw new OPServerException("请检查 animalTypeService 服务");
        }
        Map map = Maps.newHashMap();
        Long userId = UserUtil.getUserId();
        map.put("userId",userId);
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 20;
        }

        if (farmId != null){
            //如果养殖场id不为空,则根据养殖场id查询
            map.put("farm_id",farmId);
        }else {
            //如果养殖场id为空,判断地区code是否为空,不为空则根据地区查询,否则查询登陆人管辖范围的记录
            if (areaList.size() > 0){
                int i = 0;
                for (Integer areaCode : areaList) {
                    if (i == 0) {
                        map.put("province_id",areaCode);
                    } else if (i == 1) {
                        map.put("city_id",areaCode);
                    } else if (i == 2) {
                        map.put("region_id",areaCode);
                    } else if (i == 3) {
                        map.put("street_id",areaCode);
                    } else {
                        map.put("village_id",areaCode);
                    }
                    i++;
                }
            }
        }
        if (!Strings.isNullOrEmpty(startDate)){
            Date startDate1 = new Date(Long.valueOf(startDate));
            map.put("startDate",startDate1);
        }
        if (!Strings.isNullOrEmpty(endDate)){
            Date endDate1 = new Date(Long.valueOf(endDate));
            map.put("endDate",endDate1);
        }
        map.put("status", FarmRecordsNumberStatus.SUBMIT.getCode());
        if (type != null){
            map.put("type",type);
        }
        if (productType.size() > 0){
            switch (productType.size()){
                case 1:
                    List<Integer> childList = animalTypeService.queryChildCode(productType.get(0).toString());
                    map.put("breed_second_type_list",childList);
                    break;
                case 2:
                    map.put("breed_second_type",productType.get(1));
                    break;
                default:
                    break;
            }
        }
        PageInfo<FarmRecordsInspection> farmRecordsInspectionPageInfo = farmRecordsInspectionService.selectByMap(pageNum,pageSize,map);
        return Response.ok(farmRecordsInspectionPageInfo);
    }



    /**
     * 根据主键id查询对应检验检测记录
     * @param id
     * @return
     */
    @OpenMethod(key = "web.farmrecordsinspection.selectById",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET)
    public Response<FarmRecordsInspection> selectById(@RequestParam(required = false)Long id){
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求id不能为空");
        }
        try {
            if (farmRecordsInspectionService == null){
                throw new OPServerException("请检查 farmRecordsInspectionService 服务");
            }

            FarmRecordsInspection farmRecordsInspection = farmRecordsInspectionService.selectByPrimaryKey(id);
            if (farmRecordsInspection == null){
                return Response.fail(HttpCode.INTERNAL_SERVER_ERROR,"查无数据");
            }
            return Response.ok(farmRecordsInspection);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 根据主键id删除对应检验检测记录
     * @param id
     * @return
     */
    @OpenMethod(key = "web.farmrecordsinspection.deleteById",
            paramNames = {"id"},
            httpMethods = RequestMethod.POST)
    public Response<Boolean> deleteById(@RequestParam(required = false)Long id){
        if (id == null) {
            throw new OPServerException(HttpCode.BAD_REQUEST,"请求id不能为空");
        }
        try {
            if (farmRecordsInspectionService == null){
                throw new OPServerException("请检查 farmRecordsInspectionService 服务");
            }

            FarmRecordsInspection farmRecordsInspection = new FarmRecordsInspection();
            farmRecordsInspection.setId(id);
            farmRecordsInspection.setDelete_flag(FarmRecordsNumberStatus.INVALID.getCode());
            farmRecordsInspection.setUpdated_at(new Date());
            Integer con = farmRecordsInspectionService.updateByPrimaryKeySelective(farmRecordsInspection);
            return Response.ok(con == 0 ? false : true);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.DATA_DELETE_FAIL,HttpCode.DATA_DELETE_FAIL.getMessage());
        }
    }
}
